<template>
	<div class="system-dictionary-container">
		<el-card shadow="hover" class="mb10" v-auth="'system:dictionary:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.dictName" placeholder="请输入字典名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'system:dictionary:add'" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddDictionary()">新增</el-button>
			<el-table ref="tableDictionaryRef" stripe style="width: 100%" row-key="id" 
				:key="tableDictionaryKey"
				:data="tableData.data"
				:lazy="true" 
				:load="onLoadTableData" 
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
				<el-table-column label="字典名称" prop="dictName" show-overflow-tooltip min-width="150"></el-table-column>
				<el-table-column label="字典键值" show-overflow-tooltip min-width="150">
					<template #default="scope">
						<span v-if="scope.row.dictLabel != ''" class="color-success">{{ scope.row.dictLabel }}</span>
						<span v-if="scope.row.dictValue != ''" class="color-primary">{{ scope.row.dictValue }}</span>
					</template>
				</el-table-column>
				<el-table-column label="是否默认" show-overflow-tooltip width="100">
					<template #default="scope">
						<span v-if="scope.row.isDefault == 'Y'" class="color-primary">是</span>
						<span v-else class="color-info">否</span>
					</template>
				</el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
						<span v-else class="color-danger">删除</span>
					</template>
				</el-table-column>
				<el-table-column label="字典排序" prop="orderNum" width="80" show-overflow-tooltip></el-table-column>
				<el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="155" 
					v-auths="['system:dictionary:add', 'system:dictionary:edit', 'system:dictionary:remove']">
					<template #default="scope">
						<el-button v-auth="'system:dictionary:add'" type="primary" link @click="onOpenAddDictionary(scope.row)">新增</el-button>
						<el-button v-auth="'system:dictionary:edit'" type="primary" link @click="onOpenEditDictionary(scope.row)">修改</el-button>
						<el-button v-auth="'system:dictionary:remove'" type="primary" link @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddDictionary ref="addDictionaryRef" @submit="onTableSearch"/>
		<EditDictionary ref="editDictionaryRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddDictionary from '/@/views/system/dictionary/addDictionary.vue';
import EditDictionary from '/@/views/system/dictionary/editDictionary.vue';
import { getDictionaryList, delDictionary } from '/@/api/system/dictionary/index';
export default {
	name: 'dictionary',
	components: { AddDictionary, EditDictionary },
	setup() {
		const tableDictionaryRef = ref();
		const addDictionaryRef = ref();
		const editDictionaryRef = ref();
		const state = reactive({
			tableDictionaryKey: 0,
			search:{
				dictName: undefined,
			},
			tableData: {
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params: any) => {
			state.tableDictionaryKey++;
			getDictionaryList(params).then((res: any) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
				}
			});
		}
		
		const onLoadTableData = (row: any, treeNode, resolve: any) => {
			getDictionaryList({parentId: row.id}).then((res: any) => {
				if(res.code == 0){
					resolve(res.data);
				}
			});
		}

		//查询
		const onTableSearch = () => {
			if(state.search.dictName){
				loadTableData(state.search);
			}else{
				loadTableData({parentId: 'root'});
			}
		}
		//重置
		const onResetSearch = () => {
			state.search.dictName = undefined;
			loadTableData({parentId: 'root'});
		}
		// 打开新增字典弹窗
		const onOpenAddDictionary = (row: object) => {
			addDictionaryRef.value.openDialog(row);
		};
		// 打开编辑字典弹窗
		const onOpenEditDictionary = (row: object) => {
			editDictionaryRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.dictName + '】字典信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delDictionary([row.id]).then((res: any) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						onTableSearch();
					}
				});
			}).catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			loadTableData({parentId: 'root'});
		});
		return {
			tableDictionaryRef,
			addDictionaryRef,
			editDictionaryRef,
			onTableSearch,
			onResetSearch,
			onLoadTableData,
			onOpenAddDictionary,
			onOpenEditDictionary,
			onTabelRowDel,
			...toRefs(state),
		};
	},
};
</script>